﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Common;
using IServices;
using Models;
using Models.DbModels;
using Models.Dtos;
using SqlSugar;
using SqlSugar.IOC;

namespace Services
{
    public class UserServices : Repository<User>, IUserServices
    {
        public List<User> GetUsers()
        {
           return  base.Context.Queryable<User>().ToList();
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ApiResult<object>> UserInsertAsync(UserDto model) 
        {
            var result = new ApiResult<object>();
            if (string.IsNullOrWhiteSpace(model.Code) || string.IsNullOrWhiteSpace(model.Name) || string.IsNullOrWhiteSpace(model.Password))
            {
                result.Code = -1;
                result.Msg = "参数不全";
                return result;
            }

            var user = new User();
            user.Code = model.Code;
            user.Name = model.Name;
            user.Password = Common.Helper.MD5Helper.MD5Encrypt32(model.Password);

            if (!await base.InsertAsync(user))
            {
                result.Code = -1;
                result.Msg = "添加失败";
                return result;
            }

            return result;
        }


    }
}
